# -*- coding: utf-8 -*-
from api import api, ERROR_ANSWER, GlobalError
from flask import jsonify, request
from model.answer import Answer


class Error:
    ANSWER_TYPE_INVALIE = {
        'err': ERROR_ANSWER + 1,
        'msg': 'Answer Type Invalid'
    }


@api.route('/answer/<answer_id>', methods=['PUT'])
def update_answer(answer_id):
    '''
## 追问

    PUT /api/answer/<answer_id>

Parameters:

* `content` (string, required) - content of answer
* `aType` (int, required) - 2表示画室，1 表示老师，0表示学生

Errors: `3`
    '''

    content = request.form.get('content', '')
    a_type = request.form.get('aType', 1, type=int)

    if a_type != 0 and a_type != 1 and a_type != 2:
        return jsonify(stat=0, **Error.ANSWER_TYPE_INVALIE), 403

    try:
        Answer.update_answer(answer_id, content, a_type)
    except:
        return jsonify(stat=0, **GlobalError.INVALID_ANSWER_ID), 403

    return jsonify(stat=1)
